﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace Bohdan.Arch.Messaging.Special
{
    public class WaitingMiddlemanMessage : GeneralMessage
    {
        public override Guid TargetSubscriberId
        {
            get { return TimeMeasurerSubscriber._Id; }
        }

        public WaitingMiddlemanMessage(GeneralMessage messageToWait)
        {
            this.MessageToWait = messageToWait;
        }

        public GeneralMessage MessageToWait { get; set; }

        public override bool WaitCompleted()
        {
            return base.WaitCompleted() && MessageToWait.WaitCompleted();
        }

        public override bool WaitCompleted(TimeSpan ts)
        {
            Stopwatch sw = null;

            sw = Stopwatch.StartNew();
            if (!base.WaitCompleted(ts))
                return false;
            sw.Stop();
            ts = ts.Subtract(sw.Elapsed);

            return MessageToWait.WaitCompleted(ts);
        }
    }
}
